<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateShopTradeOrderTable extends Migrator
{
    /**
     * Migrate Up.
     */
    public function up()
    {
        $table = $this->table('shop_trade_order', ['comment' => '交易订单', 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci']);
        $table->addColumn(Column::enum('type', ['recharge'])->setNull(true)->setDefault(null)->setComment('订单类型:recharge=余额充值'))
            ->addColumn('order_sn', 'string', ['limit' =>  60, 'default' => '', 'comment' => '订单号'])
            ->addColumn('user_id', 'integer', ['limit' =>  11, 'default' => 0, 'comment' => '用户'])
            ->addColumn(Column::enum('status', ['closed', 'cancel', 'unpaid', 'paid', 'completed'])->setDefault('unpaid')->setComment('订单状态:closed=交易关闭,cancel=已取消,unpaid=未支付,paid=已支付,completed=已完成'))
            ->addColumn('remark', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => '用户备注'])
            ->addColumn('order_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '订单总金额'])
            ->addColumn('pay_fee', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '支付总金额'])
            ->addColumn('remain_pay_fee', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '剩余支付金额'])
            ->addColumn('paid_time', 'integer', ['limit'  =>  10, 'null' => true, 'default' => null, 'comment' => '支付成功时间'])
            ->addColumn('ext', 'string', ['limit' => 2048, 'null' => true, 'default' => null, 'comment' => '附加信息'])
            ->addColumn(Column::enum('platform', ['H5', 'App', 'WechatOfficialAccount', 'WechatMiniProgram'])->setNull(true)->setDefault(null)->setComment('平台:H5=H5,WechatOfficialAccount=微信公众号,WechatMiniProgram=微信小程序,App=App'))
            ->addColumn('create_time', 'integer', ['limit'  =>  10, 'null' => true, 'default' => null, 'comment' => '创建时间'])
            ->addColumn('update_time', 'integer', ['limit'  => 10, 'null' => true, 'default' => null, 'comment' => '更新时间'])
            ->addColumn('delete_time', 'integer', ['limit'  => 10, 'null' => true, 'default' => null, 'comment' => '删除时间'])
            ->addIndex('order_sn', ['unique'  =>  true])
            ->addIndex('user_id')
            ->addIndex('status')
            ->addIndex('create_time')
            ->create();
    }

    /**
     * Migrate Down.
     */
    public function down()
    {
        $table = $this->table('shop_trade_order');
        $table->drop();
    }
}
